package com.future.taskdemo.CompletionStage;

import org.springframework.core.io.ClassPathResource;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SqlMappingManager {


    public static void main(String[] args) throws IOException {
        new SqlMappingManager().readXml();
    }

    public static Document useDomReadXml(File file) {
//        File file = new File(soucePath);
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(file);
            return doc;
        } catch (Exception e) {
            System.err.println("读取该xml文件失败");
            e.printStackTrace();
        }
        return null;
    }

    private void readXml() throws IOException {
        try {
            ClassPathResource cl = new ClassPathResource("sql-mapping.xml");
//        ClassPathResource cl = new ClassPathResource("test.xml");
            String soucePath = "E:\\AllFilesISHere\\testfile\\books.xml";
            File file = cl.getFile();
            Document doc = SqlMappingManager.useDomReadXml(file);
            //读取xml内部节点集合
            org.w3c.dom.NodeList nlst = doc.getElementsByTagName("SqlMapping");
            PreparedStatement ps = null;
            ResultSet rs = null;
            //遍历集合内容
            for (int i = 0; i < nlst.getLength(); i++) {

                String businessDataitemIds = doc.getElementsByTagName("businessDataitemIds").item(i).getFirstChild().getNodeValue().trim();
                String pnType = doc.getElementsByTagName("pnType").item(i).getFirstChild().getNodeValue().trim();
                String projectName = doc.getElementsByTagName("projectName").item(i).getFirstChild().getNodeValue().trim();
                String topicName = doc.getElementsByTagName("topicName").item(i).getFirstChild().getNodeValue().trim();
                String shardCount = doc.getElementsByTagName("shardCount").item(i).getFirstChild().getNodeValue().trim();
                String lifeCycle = doc.getElementsByTagName("lifeCycle").item(i).getFirstChild().getNodeValue().trim();
                String fields = doc.getElementsByTagName("fields").item(i).getFirstChild().getNodeValue().trim();

                String upperCase = topicName.toUpperCase();

                String replace = upperCase.replace("_SOURCE", "");
                System.out.println(businessDataitemIds);


                System.out.println(replace);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}